/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rdb.sbean;

import com.rdb.entities.Exams;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TemporalType;

/**
 *
 * @author Bich Tran Van
 */
@Stateless
public class ExamsFacade extends AbstractFacade<Exams> {
    @PersistenceContext(unitName = "ResultDeclarationBoard-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public ExamsFacade() {
        super(Exams.class);
    }
    
    public List<Exams> getList(){
        String strQuery = "SELECT e FROM Exams e order by e.id DESC";
        javax.persistence.Query q = getEntityManager().createQuery(strQuery);
        return q.getResultList();
    }
    
    public List<Exams> searchExam(int facId, int subjectId, Date fromDate, Date toDate){
        String strQuery = "SELECT e FROM  Exams e WHERE 1= 1 ";
        if(facId != 0){
            strQuery = strQuery + " and e.assignFac.userId = :userId";
        }
        if(subjectId != 0){
            strQuery = strQuery + " and e.subjectId.id = :subjectId";
        }
        
        if(fromDate != null){
            strQuery = strQuery + " and e.dateStart >= :fromdate";
        }
        
        if(toDate != null){
            strQuery = strQuery + " and e.dateStart <= :todate";
        }
        strQuery = strQuery + "  order by e.id DESC";
        javax.persistence.Query q = getEntityManager().createQuery(strQuery);
        if(facId != 0){
            q.setParameter("userId", facId);
        }
        if(subjectId != 0){
            q.setParameter("subjectId", subjectId);
        }
        
        if(fromDate != null){
            q.setParameter("fromdate", fromDate,TemporalType.DATE);
        }
        
        if(toDate != null){
            q.setParameter("todate", toDate,TemporalType.DATE);
        }
        return q.getResultList();
    }
    
    public Exams getByID(int id){
        String strQuery = "SELECT e FROM Exams e WHERE e.id = :id order by e.id DESC";
        javax.persistence.Query q = getEntityManager().createQuery(strQuery);
        q.setParameter("id", id);
        Exams exams = (Exams) q.getSingleResult();
        return exams;
    }
    
    
}
